package com.traffic.util;

import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

public class NormalStrategy implements ExclusionStrategy{
	private static Logger log = LoggerFactory.getLogger(NormalStrategy.class);
	private Class<?> target;   
	private String[] fields;   
	private Class<?> clazz;  
	private String[]   clazzFields;   

	private boolean reverse;   

	public NormalStrategy(Class<?> target) {
		super();
		this.target = target;
	}

	@Override
	public boolean shouldSkipClass(Class<?> class1) {
		return false;
	}

	@Override
	public boolean shouldSkipField(FieldAttributes fieldattributes) {
		Class<?> owner = fieldattributes.getDeclaringClass();
		Class<?> c = fieldattributes.getDeclaredClass();
		String f = fieldattributes.getName();
		boolean isSkip = false;
		//Ŀ��class
		if (owner == target) {
			if (ArrayUtils.contains(fields, f)) {
				log.debug("fitler field:{} for class:{}", f, owner);
				isSkip = true;
			}
		}
		//���⴦��class
		else if(owner == clazz){
			if(ArrayUtils.contains(clazzFields, f)){
				isSkip = true;
			}
		}
		if (reverse) {
			isSkip = !isSkip;
		}
		return isSkip;
	}

	public void setFields(String[] fields) {
		this.fields = fields;
	}

	public void setClazz(Class<?> clazz) {
		this.clazz = clazz;
	}
	public String[] getClazzFields() {
		return clazzFields;
	}

	public void setClazzFields(String[] clazzFields) {
		this.clazzFields = clazzFields;
	}
	public void setReverse(boolean reverse) {
		this.reverse = reverse;
	}
}
